﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CXMLStudioIDE.Views;
using System.Windows.Forms;
using System.Data;
using CXMLStudioIDE.Common.Entities;
using CXMLStudioIDE.Forms;

namespace CXMLStudioIDE.Presenters
{
    public class SolutionExplorerPresenter
    {
        #region Variable declaration
        private ISolutionExplorerView view;
        #endregion

        #region Constructor
        public SolutionExplorerPresenter(ISolutionExplorerView view)
        {
            this.view = view;
            view.OnNodeMouseClick += new TreeNodeMouseClickEventHandler(OnNodeMouseClick);
            view.OnAddNewProject += new EventHandler(OnAddNewProject);
            view.OnAddExistingProject += new EventHandler(OnAddExistingProject);
            view.OnRemove += new EventHandler(OnRemove);
            view.OnAddExistingBE += new EventHandler(OnAddExistingBE);
            view.OnAddNewBE += new EventHandler(OnAddNewBE);
            view.OnPropertiesClick += new EventHandler(OnPropertiesClick);
        }
        #endregion

        #region Methods
        public void PopulateTreeView(int parentID, DataTable projects, Solution solution)
        {
            bool hasSolution = (solution == null ? false : true);
            view.ClearNodes();
            PopulateTreeView(view.Nodes, parentID, projects, hasSolution);
        }

        private void PopulateTreeView(TreeNodeCollection parentNode, int parentID, DataTable projects, bool hasSolution)
        {
            TreeNodeCollection newParentNode = null;
            foreach (DataRow project in projects.Rows)
            {
                if (Convert.ToInt32(project["parentID"]) == parentID)
                {
                    String key = project["ID"].ToString();
                    String text = project["title"].ToString();
                    if (Convert.ToInt32(project["parentID"]) == 0)
                    {
                        int image_index = (hasSolution ? 0 : 3);
                        TreeNode tn = parentNode.Add(key, text, image_index);
                        tn.SelectedImageIndex = image_index;
                        newParentNode = tn.Nodes;
                    }
                    else
                    {
                        int image_index = (Convert.ToInt32(project["parentID"]) == 1 && hasSolution ? 3 : 7);
                        if (text.StartsWith("Scope") || text.StartsWith("Context") || text.StartsWith("Business Entities"))
                            image_index = 1;
                        //Not ideal hack to set the selected index = image index
                        TreeNode tn = parentNode.Add(key, text, image_index);
                        tn.SelectedImageIndex = image_index;
                        newParentNode = tn.Nodes;
                    }
                    PopulateTreeView(newParentNode, Convert.ToInt32(project["ID"]), projects, hasSolution);
                }
            }
            //Expand all treeview
            view.ExpandAllTreeView();
            //this.treeView1.ExpandAll();
        }

        public void OnNodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {

        }

        public void OnAddNewProject(object sender, EventArgs e)
        {
            NewProjectForm npf = new NewProjectForm(view.ParentMainForm);
            npf.Show();
        }
        public void OnAddExistingProject(object sender, EventArgs e)
        {
            NewProjectFromExistingForm apf = new NewProjectFromExistingForm(view.ParentMainForm);
            apf.Show();
        }

        public void OnRemove(object sender, EventArgs e)
        {
            string sZ_SelectedNodeText = view.SelectedNodeText;
            if (sZ_SelectedNodeText.StartsWith("Project"))
            {
                Project p = view.ParentMainForm.m_SolutionManager.m_solution.Projects.GetProjectByTreeviewID(Convert.ToInt32(view.SelectedNodeName));
                DialogResult result = MessageBox.Show(view.ParentMainForm, "Are you sure you want to remove Project: " + p.Name + " ?",
                        "CXMLStudioIDE - Confirm close Solution", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    view.ParentMainForm.m_SolutionManager.RemoveProjectFromSolution(p);
                    view.ParentMainForm.UpdateMainForm();
                }
            }
            if (!sZ_SelectedNodeText.StartsWith("Solution") &&
                !sZ_SelectedNodeText.StartsWith("Project") &&
                !sZ_SelectedNodeText.StartsWith("Business Entities"))
            {
                if (view.ParentMainForm.m_SolutionManager.m_solution.Projects != null)
                {
                    Project p = ProjectCollection.GetProjectContainingBusinessEntityByTreeViewID(view.ParentMainForm.m_SolutionManager.m_solution.Projects, Convert.ToInt32(view.SelectedNodeName));
                    if (p != null)
                    {
                        BusinessEntity b = p.GetBusinessEntityByTreeViewID(Convert.ToInt32(view.SelectedNodeName));
                        view.ParentMainForm.m_SolutionManager.RemoveBusinessEntityFromProject(p, b);
                        view.ParentMainForm.UpdateMainForm();
                    }
                }
            }

        }

        public void OnAddExistingBE(object sender, EventArgs e)
        {
            AddExistingEntityForm aef = null;
            //if clicked on project node then send treeviewID for project
            //if clicked on business entities then send the correct treeviewID for the project
            if (view.SelectedNodeText.StartsWith("Project"))
                aef = new AddExistingEntityForm(view.ParentMainForm, Convert.ToInt32(view.SelectedNodeName));
            else if (view.SelectedNodeText.StartsWith("Business Entities"))
                aef = new AddExistingEntityForm(view.ParentMainForm, Convert.ToInt32(view.SelectedNodeName) - 1);
            if (aef != null)
                aef.Show();
        }

        public void OnAddNewBE(object sender, EventArgs e)
        {
            AddNewEntityForm anef = null;
            if (view.SelectedNodeText.StartsWith("Project"))
                anef = new AddNewEntityForm(view.ParentMainForm, Convert.ToInt32(view.SelectedNodeName));
            else if (view.SelectedNodeText.StartsWith("Business Entities"))
                anef = new AddNewEntityForm(view.ParentMainForm, Convert.ToInt32(view.SelectedNodeName) - 1);
            if (anef != null)
                anef.Show();
        }

        public void OnPropertiesClick(object sender, EventArgs e)
        {
            if (view.SelectedNodeText.StartsWith("Project"))
            {
                view.ParentMainForm.LoadProjectProperties(Convert.ToInt32(view.SelectedNodeName));
            }
            if (view.SelectedNodeText.StartsWith("Solution"))
            {
                view.ParentMainForm.LoadSolutionProperties();
            }
        }
        #endregion
    }
}
